package com.zsrt.java.ex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;
import java.util.Vector;

import org.junit.Test;

public class ArrayListTest {
	
	@Test
	public void testName() throws Exception {
		
		List<Object> list=new ArrayList<Object>();
		
		list.add(new Integer(1));
		list.add(new Integer(5));
		
		//容许重复
		list.add(new Integer(1));
		list.add("A");
		list.add(new Double(1.0));
		
		//允许元素为空
		list.add(null);
		//拿到第五个元素
//		System.out.println(list.get(4));
		
		//ArrayList的第一种迭代方式 get(index)
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		
		list.remove(1);
		System.out.println("================");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		
		list.remove(new Integer(1));
		System.out.println("================");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		/**
		 * remove可以任意删除指定下标  指定元素
		 */
		list.remove("A");
		System.out.println("================");
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
		
		
		//通过Iterator 迭代遍历
		System.out.println("================");
		for(Iterator iter=list.iterator(); iter.hasNext(); ){
			Object o = (Object)iter.next();
			System.out.println(o);
		}
		
		
		
		
		
	}

	
	@Test
	public void LinkList() throws Exception {
		
		LinkedList<Object> linkList=new LinkedList<Object>();
		
		linkList.add(true);
		linkList.addFirst("java");
		linkList.add(false);
		linkList.addLast("jsp");
		
		System.out.println(linkList.getLast());
		
		
		for(Iterator iter=linkList.iterator();iter.hasNext();){
			Object o=iter.next();
			System.out.println(o);
		}
	}
	
	
	@Test
	public void Vector() throws Exception {
		
		
		Vector<Object> v=new Vector<Object>();
		
		v.add(1);
		v.add(1.0);
		v.add("A");
		v.add(new Boolean(false));
		
//		System.out.println(v.get(2));
		
		for(Iterator iterator=v.iterator(); iterator.hasNext();){
			
			Object obj=(Object)iterator.next();
			System.out.println(obj);
		}
		
	}
	
	/**
	 * 后进先出
	 * @throws Exception
	 */
	@Test
	public void Stack() throws Exception {
		Stack s=new Stack();
		s.push(1);
		s.push(2);
		s.push(3);
		s.push(4);
		
		while(!s.empty()){
			System.out.println(s.peek());
			s.pop();
		}
		
	}
	
	/**
	 * 不能放整形  重复数据  无序
	 * @throws Exception
	 */
	@Test
	public void HashSet() throws Exception {
		
		HashSet hs=new HashSet();
		
		hs.add("1");
		hs.add(1);
//		hs.add(1);
		hs.add("1");
		System.out.println("哈希表的大小："+hs.size());
		
		for(Iterator iterator=hs.iterator();iterator.hasNext();){
			String str=(String)iterator.next();
			System.out.println(str);
		}
		
	}
	/**
	 * 按照元素自然排列
	 * @throws Exception
	 */
	@Test
	public void TreeSet() throws Exception {
		//set是不能存放相同的数据
		TreeSet<Integer> hs=new TreeSet<Integer>();
		hs.add(5);
		hs.add(5);
		hs.add(3);
		
		boolean con = hs.contains(5);
		System.out.println(con);
		System.out.println("哈希表的大小："+hs.size());
		
		for(Iterator iterator=hs.iterator();iterator.hasNext();){
			Integer str=(Integer)iterator.next();
			System.out.println(str);
		}
		
		
	}
	/**
	 * next();方法返回值是String  所以key 要为String类型
	 * @throws Exception
	 */
	@Test
	public void MapTest() throws Exception {
		
		HashMap map=new HashMap();
		
		map.put("1", "java");
		map.put("2", "jsp");
		map.put(null, "?");
		map.put(null, 1);
		map.put("3", null);
		
		boolean b = map.containsKey(null);
		System.out.println(b);
		
		for(Iterator<String> i=map.keySet().iterator();i.hasNext();){
			String key=i.next();
			Object value = map.get(key);
			System.out.println(key+"-----"+value);
			
		}
		
		
		
		//后进先出
		Hashtable<String,String> htable=new Hashtable<String,String>();
		htable.put("1", "小黄书");
		htable.put("2", "武林秘籍");
//		htable.put(null, "?");不允许为空
//		htable.put("3", null);不允许为空
		
		for(Iterator iter=htable.keySet().iterator();iter.hasNext();){
			
			String key=(String)iter.next();
			String value=htable.get(key);
			System.out.println(key+"--------"+value);
			
		}
		
	}
	
	
	@Test
	public void TestTreeSet() throws Exception {
		
		TreeSet<String> ts=new TreeSet<String>();
		ts.add("Tom");
		ts.add("Jerry");
		ts.add("Dog");
		ts.addAll(ts);
		ts.add("aaa");
		
		int size = ts.size();
		System.out.println(size);
		for(Iterator iterator=ts.iterator();iterator.hasNext();){
			
			Object nt = iterator.next();
			System.out.println(nt);
		}
	}
	
	
	@Test
	public void HashMap() throws Exception {
	
		HashMap map=new HashMap();
		java.util.HashMap map2 = map;
		map2.put("张三", "狗蛋");
		map2.put("李四", "狗娃");
		Set keys=map2.keySet();
		for(Iterator iterator=keys.iterator();iterator.hasNext();){
			
			Integer next = (Integer) iterator.next();
			Integer next2 = next;
			 map2.get(next2);
		}
		
		
		
	}
	
	
	
	
	
}
